package top.ddandang.blog.controller;


import top.ddandang.blog.bean.GlobalResponse;
import top.ddandang.blog.model.pojo.Draft;
import top.ddandang.blog.model.vo.DraftVo;
import top.ddandang.blog.service.DraftService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author D
 * @since 2020-06-26
 */
@RestController
@RequestMapping("/draft")
public class DraftController {

    @Resource
    private DraftService draftService;

    @PostMapping("/insertDraft")
    public GlobalResponse insertDraft(@RequestBody DraftVo draftVo) {
        int draftId = draftService.insertDraft(draftVo);
        return GlobalResponse.success().data("draftId", draftId).message("插入成功！");
    }

    @PostMapping("/updateDraft")
    public GlobalResponse updateDraft(@RequestBody DraftVo draftVo) {
        draftService.updateDraft(draftVo);
        return GlobalResponse.success().message("更新成功！");
    }

    @GetMapping("/updateDraft")
    public GlobalResponse getDraftById(Integer id) {
        Draft draft = draftService.getDraftById(id);
        return GlobalResponse.success().data("draft", draft).message("查询成功！");
    }

    @GetMapping("/listDraft")
    public GlobalResponse listDraft() {
        List<Draft> drafts = draftService.listDraft();
        return GlobalResponse.success().data("drafts", drafts).message("查询成功！");
    }

    @DeleteMapping("/deletedDraft")
    public GlobalResponse deletedDraft(Integer id) {

        draftService.deleteDraft(id);
        return GlobalResponse.success().message("删除成功！");
    }

    @GetMapping("/judgeDraft")
    public GlobalResponse judgeDraft(Integer articleId) {
        return GlobalResponse.success().data("draftId", draftService.judgeDraft(articleId));
    }

}

